using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Sec_scan漏洞扫描系统
{
    public partial class Sec_ScanSystem : Form
    {
        public Sec_ScanSystem()
        {
            InitializeComponent();
        }

        private void checkBox11_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox11.Checked == true)
            {
                checkBox12.Checked = false;
                textBox1.Enabled = true;
                textBox2.Enabled = true;
            }
        }

        private void checkBox12_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox12.Checked == true)
            {
                checkBox11.Checked = false;
                textBox1.Enabled = false;
                textBox2.Enabled = false;
            }
        }

        private void Sec_ScanSystem_Load(object sender, EventArgs e)
        {
            //加载系统时 同时加载系统配置
            if(checkBox12.Checked == true)
            {
                checkBox11.Checked = false;
                textBox1.Enabled = false;
                textBox2.Enabled = false;
            }
            else
            {
                checkBox12.Checked = false;
                textBox1.Enabled = true;
                textBox2.Enabled = true;
            }
            //刷新整体扫描配置
            ReferScanSeetring();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //添加端口
            if(string.IsNullOrEmpty(textBox3.Text) || string.IsNullOrEmpty(textBox4.Text))
            {
                MessageBox.Show("数据不能为空！");
            }
            else
            {
                //执行数据添加
                string path = Sec_Api.Sec_AllAPI.Sqlclient(Sec_Api.Sec_SystemConfigAllAPI.P1);//因为走的是系统设置这块
                SQLiteConnection connection = new SQLiteConnection(path);
                try
                {
                    connection.Open();
                    if(connection.State == ConnectionState.Open)
                    {
                        string sql = "INSERT INTO Sec_PortScan_Dispose (port, portnoite, addtime) VALUES('"+textBox3.Text+"', '"+textBox4.Text+"', '"+DateTime.Now+"')";
                        SQLiteCommand command = new SQLiteCommand(sql,connection);
                        command.ExecuteReader();
                        MessageBox.Show("端口数据添加成功！");
                        textBox3.Clear();
                        textBox4.Clear();
                        //执行指定端口配置
                        ReferLoopInfo();
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
        /// <summary>
        /// 刷新指定端口配置
        /// </summary>
        public void ReferLoopInfo()
        {
            dataGridView1.Rows.Clear();//每一次刷新都进行清空
            string sql = Sec_Api.Sec_AllAPI.Sqlclient(Sec_Api.Sec_LoopScan_Service_Api.P1);
            SQLiteConnection connection = new SQLiteConnection(sql);
            try
            {
                connection.Open();
                if (connection.State == ConnectionState.Open)
                {
                    string sqls = Sec_Api.Sec_AllAPI.PortScanDispose(Sec_Api.Sec_LoopScan_Service_Api.P1);
                    SQLiteCommand command = new SQLiteCommand(sqls, connection);
                    using (SQLiteDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (dr.Read())
                        {
                            //获取数据
                            string id = dr["ID"].ToString();//主键
                            string ports = dr["port"].ToString();//漏洞名称
                            string portsnoite = dr["portnoite"].ToString();//有效载荷
                            dataGridView1.Rows.Add(id, ports,portsnoite);
                        }
                    }
                    int Dins = dataGridView1.Rows.Count;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //刷新端口配置
            ReferLoopInfo();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //更新配置
            string scan1 = checkBox1.Checked ? "1" : "0";
            string scan2 = checkBox2.Checked ? "1" : "0";
            string scan3 = checkBox3.Checked ? "1" : "0";
            string scan4 = checkBox4.Checked ? "1" : "0";
            string scan5 = checkBox5.Checked ? "1" : "0";
            string scan6 = checkBox6.Checked ? "1" : "0";
            string scan7 = checkBox7.Checked ? "1" : "0";
            string scan8 = checkBox8.Checked ? "1" : "0";
            string scan9 = checkBox9.Checked ? "1" : "0";
            string scan10 = checkBox10.Checked ? "1" : "0";
            string scan11 = checkBox11.Checked ? "1" : "0";
            string scan12 = checkBox12.Checked ? "1" : "0";
            string scantype = comboBox1.Text;
            string sql = Sec_Api.Sec_AllAPI.Sqlclient(Sec_Api.Sec_LoopScan_Service_Api.P1);
            SQLiteConnection connection = new SQLiteConnection(sql);
            try
            {
                connection.Open();
                if (connection.State == ConnectionState.Open)
                {
                    string sqls = "UPDATE Sec_Scan_Seetring SET info='"+scan1+ "', adminscan='" + scan2 + "', domainscan='" + scan3 + "', loopscan='" + scan4 + "', fingerscan='" + scan5 + "', mentinfo='" + scan6 + "', sqlinfo='" + scan7 + "', systeminfo='" + scan8 + "', portinfo='" + scan9 + "', noinfo='" + scan10 + "', allport='" + scan11 + "', pointport='" + scan12 + "', scantype='"+scantype+"', updata='" + DateTime.Now+"' WHERE ID=1";
                    SQLiteCommand command = new SQLiteCommand(sqls, connection);
                    command.ExecuteReader();
                    MessageBox.Show("扫描配置更新成功！");
                    //刷新配置
                    ReferScanSeetring();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        /// <summary>
        /// 刷新扫描设置
        /// </summary>
        public void ReferScanSeetring()
        {
            string sql = Sec_Api.Sec_AllAPI.Sqlclient(Sec_Api.Sec_LoopScan_Service_Api.P1);
            SQLiteConnection connection = new SQLiteConnection(sql);
            try
            {
                connection.Open();
                string sqls = Sec_Api.Sec_AllAPI.ScanSeetring(Sec_Api.Sec_LoopNumberApi.M1);
                SQLiteCommand command = new SQLiteCommand(sqls,connection);
                using(SQLiteDataReader dr = command.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        string scan1 = dr["info"].ToString();
                        string scan2 = dr["adminscan"].ToString();
                        string scan3 = dr["domainscan"].ToString();
                        string scan4 = dr["loopscan"].ToString();
                        string scan5 = dr["fingerscan"].ToString();
                        string scan6 = dr["mentinfo"].ToString();
                        string scan7 = dr["sqlinfo"].ToString();
                        string scan8 = dr["systeminfo"].ToString();
                        string scan9 = dr["portinfo"].ToString();
                        string scan10 = dr["noinfo"].ToString();
                        string scan11 = dr["allport"].ToString();
                        string scan12 = dr["pointport"].ToString();
                        string scantype = dr["scantype"].ToString();
                        if (scan1.ToString() == "1")
                        {
                            checkBox1.Checked = true;
                        }
                        else
                        {
                            checkBox1.Checked = false;
                        }
                        if (scan2.ToString() == "1")
                        {
                            checkBox2.Checked = true;
                        }
                        else
                        {
                            checkBox2.Checked = false;
                        }
                        if (scan3.ToString() == "1")
                        {
                            checkBox3.Checked = true;
                        }
                        else
                        {
                            checkBox3.Checked = false;
                        }
                        if (scan4.ToString() == "1")
                        {
                            checkBox4.Checked = true;
                        }
                        else
                        {
                            checkBox4.Checked = false;
                        }
                        if (scan5.ToString() == "1")
                        {
                            checkBox5.Checked = true;
                        }
                        else
                        {
                            checkBox5.Checked = false;
                        }
                        if (scan6.ToString() == "1")
                        {
                            checkBox6.Checked = true;
                        }
                        else
                        {
                            checkBox6.Checked = false;
                        }
                        if (scan7.ToString() == "1")
                        {
                            checkBox7.Checked = true;
                        }
                        else
                        {
                            checkBox7.Checked = false;
                        }
                        if (scan8.ToString() == "1")
                        {
                            checkBox8.Checked = true;
                        }
                        else
                        {
                            checkBox8.Checked = false;
                        }
                        if (scan9.ToString() == "1")
                        {
                            checkBox9.Checked = true;
                        }
                        else
                        {
                            checkBox9.Checked = false;
                        }
                        if (scan10.ToString() == "1")
                        {
                            checkBox10.Checked = true;
                        }
                        else
                        {
                            checkBox10.Checked = false;
                        }
                        if (scan11.ToString() == "1")
                        {
                            checkBox11.Checked = true;
                        }
                        else
                        {
                            checkBox11.Checked = false;
                        }
                        if (scan12.ToString() == "1")
                        {
                            checkBox12.Checked = true;
                        }
                        else
                        {
                            checkBox12.Checked = false;
                        }
                        if(scantype.ToString() == "TCP扫描")
                        {
                            comboBox1.Text = "TCP扫描";
                        }
                        else
                        {
                            comboBox1.Text = "SYN扫描";
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
